<?php echo $this -> Session -> flash(); ?>
<?php if ($is_admin): ?>
<div id="TB_load">
	<?php echo $this->Html->image("loading_animation.gif")?>
</div>
<div class="sale_item">
	<h2 style="padding:0.5%;"><?php echo __('Markdown');
	?></h2>
	<?php echo $this -> Form -> create('Customer'); ?>
	<table class="items">
		<thead>
			<tr>
				<th width="5%"><?php echo __('Item Id'); ?></th>
				<th width="30%"><?php echo __('Item Name'); ?></th>
				<th width="15%"><?php echo __('Original Price'); ?></th>
				<th width="15%"><?php echo __('Markdown Price'); ?></th>
				<th width="15%"><?php echo __('Confirm'); ?> <!--<input type="checkbox" id="selectall"/>--></th>
				<th width="20%"><?php echo __('On Display Date'); ?></th>
				<th width="15%"><?php echo __('Dismiss'); ?></th>
			</tr>
		</thead>
		<tbody>
			<?php if(!empty($items)){
			$i = 1; ?>
			<?php foreach($items as $item){?>
			<tr id="sale_id_<?php echo $item["Item"]["id"]?>" class="rowOfItem" data-id="<?php echo $item["Item"]["id"]?>">
				<td ><?php echo $item["Item"]["id"]?></td>
				<td><?php echo $item["Item"]["name"]?></td>
				<td><?php echo $item["Item"]["original_price"]?></td>
				<td>
				<div class="itemPriceDiv">
					<div class="itemPriceText">
						<?php echo $item["Item"]["current_price"]; ?>
					</div>
					<div class="itemPriceTextboxDiv">
						<input class="itemPriceTextbox" onkeypress="return isNumberKey(event)" style="display: none" type="textbox" name="itemPriceTextbox" value="<?=$item["Item"]["current_price"] ?>" />
					</div>
				</div></td>
				<td>
				<input name=""  value="1"  type="checkbox" class="itemPrice">
				</td>
				<td><?php echo $item["Item"]["on_display_date"]?></td>
				<td>
				<input name="itemDismiss"  value="1"  type="checkbox" data-id="<?=$item["Item"]["id"] ?>" class="itemDismiss">
				</td>
			</tr>
			<?php $i++; } ?>
			<tr>
				<td colspan="7">
				<input  onclick="if (confirm('Are you sure you want to submit')) { postValues(); } event.returnValue = false; return false;"  type="button" name="submit" class="submitButton" value="submit" />
				</td>
			</tr>
			
			<?php } else { ?>
			<tr>
				<td colspan="7" class="actions"> No Items Available </td>
				</tr>
				<?php } ?>
				</tbody>
				</table>
				<div class="paging">
				<?php
				echo $this -> Paginator -> counter(array('format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')));
				?>
				<?php
				echo $this -> Paginator -> prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
				echo $this -> Paginator -> numbers(array('separator' => ''));
				echo $this -> Paginator -> next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
				?>
				</div>
				</div>
				<div id="item_refund_dialog" title="Refund">
				<div id="item_refund_li"></div>
				</div>
				<?php else: ?>
				<p>
				YOU ARE NOT SUPPOSED TO VIEW THIS PAGE!!!
				</p>
				<?php endif; ?>
				<?php echo $this -> Html -> css('/css/ui-lightness/jquery'); ?>
				<?php echo $this -> Html -> script('jquery-ui'); ?>
				<?php echo $this -> Html -> script('jquery.form.min'); ?>
				<script type="text/javascript">
				$(document).ready(function() {

				$(".itemPrice").click(function() {
				var checkboxFlag = $(this).is(':checked');
				if (checkboxFlag) {
				var itemDismissCheck = $(this).closest('tr').find(".itemDismiss").is(':checked');
				var itemID = $(this).closest('tr').attr('data-id');
				if (itemDismissCheck) {
				$(this).removeAttr('checked');
				alert('Please uncheck DISMISS column for Item-' + itemID);
				return;
				}
				$(this).closest('tr').find(".itemPriceTextbox").css('display', 'block');
				$(this).closest('tr').find(".itemPriceText").css('display', 'none');

				} else {
				$(this).closest('tr').find(".itemPriceTextbox").css('display', 'none');
				$(this).closest('tr').find(".itemPriceText").css('display', 'block');
				}
				if ($(".itemPrice").length == $(".itemPrice:checked").length) {
				$("#selectall").attr("checked", "checked");
				} else {
				$("#selectall").removeAttr("checked");
				}

				});

				$(".itemDismiss").click(function() {
				var checkDismiss = $(this).is(':checked');
				if (checkDismiss) {
				var itemPriceCheck = $(this).closest('tr').find(".itemPrice").is(':checked');
				var itemID = $(this).attr('data-id');
				if (itemPriceCheck) {
				$(this).removeAttr('checked');
				alert('Please uncheck CONFIRM column for Item-' + itemID);
				return;
				}
				}

				})
				//refund

				$("#selectall").click(function() {
				$('.itemPrice').attr('checked', this.checked);

				var checkboxAllFlag = $(this).is(':checked');
				if (checkboxAllFlag) {
				$(".itemPriceTextbox").css('display', 'block');
				$(".itemPriceText").css('display', 'none');

				} else {
				$(".itemPriceTextbox").css('display', 'none');
				$(".itemPriceText").css('display', 'block');

				}

				});

				});
				function isNumberKey(evt) {
				var charCode = (evt.which) ? evt.which : event.keyCode;
				if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
				return false;
				return true;
				}

				function postValues() {
				var getCheckItemLength = $("input:checkbox:checked.itemPrice, input:checkbox:checked.itemDismiss").length;
				
				if (getCheckItemLength == 0) {
				alert('Please check atleast one item');
				return false;
				}
				var jsonObj = [];
				var renderFlag = true;
				$("input:checkbox:checked.itemPrice").each(function(index) {
				var priceValue = $(this).closest('tr').find(".itemPriceTextbox").val();
				if (priceValue == "" || priceValue == null || priceValue == 'undefined') {
				renderFlag = false;
				}
				var getId = $(this).closest('tr').attr('data-id');
				jsonObj.push({
				id : getId,
				optionValue : priceValue,
				dismissId : 0
				});
				});

				$("input:checkbox:checked.itemDismiss").each(function(index) {
				var getId = $(this).closest('tr').attr('data-id');
				jsonObj.push({
				id : getId,
				optionValue : 0,
				dismissId : getId,
				});
				});

				if (!renderFlag) {
				alert('Please check Item price textbox value');
				return false;
				}
				$.ajax({
				type : "POST",
				url : '../php/item_update.php',
				data : {
				checkType : 'Yes',
				val : jsonObj
				},
				dataType : "JSON",
				success : function() {
				$("#selectall, .itemPrice").removeAttr("checked");
				location.reload();
				},
				error : function() {
				alert('Oh! No, Something Missing');
				}
				});
				}
				</script>
